/*
 * Simple Open EtherCAT Master Library 
 *
 * File    : ethercatconfig.h
 * Version : 1.3.1
 * Date    : 11-03-2015
 * Copyright (C) 2005-2015 Speciaal Machinefabriek Ketels v.o.f.
 * Copyright (C) 2005-2015 Arthur Ketels
 * Copyright (C) 2008-2009 TU/e Technische Universiteit Eindhoven
 * Copyright (C) 2014-2015 rt-labs AB , Sweden
 *
 * SOEM is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the Free
 * Software Foundation.
 *
 * SOEM is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * As a special exception, if other files instantiate templates or use macros
 * or inline functions from this file, or you compile this file and link it
 * with other works to produce a work based on this file, this file does not
 * by itself cause the resulting work to be covered by the GNU General Public
 * License. However the source code for this file must still be made available
 * in accordance with section (3) of the GNU General Public License.
 *
 * This exception does not invalidate any other reasons why a work based on
 * this file might be covered by the GNU General Public License.
 *
 * The EtherCAT Technology, the trade name and logo “EtherCAT” are the intellectual
 * property of, and protected by Beckhoff Automation GmbH. You can use SOEM for
 * the sole purpose of creating, using and/or selling or otherwise distributing
 * an EtherCAT network master provided that an EtherCAT Master License is obtained
 * from Beckhoff Automation GmbH.
 *
 * In case you did not receive a copy of the EtherCAT Master License along with
 * SOEM write to Beckhoff Automation GmbH, Eiserstraße 5, D-33415 Verl, Germany
 * (www.beckhoff.com).
 */

/** \file 
 * \brief
 * Headerfile for ethercatconfig.c 
 */

#ifndef _ethercatconfig_
#define _ethercatconfig_

#ifdef __cplusplus
extern "C"
{
#endif

#define EC_NODEOFFSET      1000
#define EC_TEMPNODE        0xffff

#define SD700_ECAT          1
#define NUMBER_OF_SLAVES   1

typedef struct
{
   uint8_t    out1;
   uint8_t    out2;
	 uint8_t    out3;
   uint8_t    out4;
	 uint8_t    out5;
   uint8_t    out6;
	 uint8_t    out7;
   uint8_t    out8;
} out_EL2008_t;

#ifdef 0
typedef __packed struct //众辰伺服控制器
{
    // 控制字，用于控制设备的运行状态等，数据类型为无符号16位整数
    uint16_t ControlWord; 
    // 目标位置，用于指定设备的目标位置，数据类型为有符号32位整数
    int32_t TargetPos; 
    // 目标速度，用于指定设备的目标速度，数据类型为有符号32位整数
    int32_t TargetVelocity; 
    // 操作模式，用于表示设备的操作模式，数据类型为有符号8位整数
    int8_t ModesOfOperation; 
    // 目标扭矩，用于指定设备的目标扭矩，数据类型为有符号16位整数
    int16_t TargetTorque; 
}PDO_Output;

typedef __packed struct //众辰伺服控制器
{
    // 状态字，用于表示设备当前状态，数据类型为无符号16位整数
    uint16_t StatusWord;
    // 实际位置值，用于记录设备当前的实际位置，数据类型为有符号32位整数
    int32_t PositionActualValue;
    // 实际速度值，用于记录设备当前的实际速度，数据类型为有符号32位整数
    int32_t VelocityActualValue;
    // 实际扭矩值，用于记录设备当前的实际扭矩，数据类型为有符号16位整数
    int16_t TorqueActualValue;
    // 操作模式显示，用于展示设备当前的操作模式，数据类型为有符号8位整数
    int8_t ModesOfOperationDisplay;
}PDO_Input;
#else
typedef __packed struct //众辰伺服控制器
{
    // 控制字，用于控制设备的运行状态等，数据类型为无符号16位整数
    uint16_t ControlWord; 
    // 目标位置，用于指定设备的目标位置，数据类型为有符号32位整数
    int32_t TargetPos; 
    // 目标速度，用于指定设备的目标速度，数据类型为有符号32位整数
    int32_t TargetVelocity; 
    // 目标扭矩，用于指定设备的目标扭矩，数据类型为有符号16位整数
    int16_t TargetTorque; 
	
    int8_t ModesOfOperation; 
		uint16_t TouchProbeFunction;
		int32_t MaxProfileVelocity;
		uint16_t PositiveTorqueLimitValue;
		uint16_t NegativeTorqueLimitValue;
}PDO_Output;

typedef __packed struct //众辰伺服控制器
{
    uint16_t ErrorCode;
		// 状态字，用于表示设备当前状态，数据类型为无符号16位整数
    uint16_t StatusWord;
    // 实际位置值，用于记录设备当前的实际位置，数据类型为有符号32位整数
    int32_t PositionActualValue;
    // 实际速度值，用于记录设备当前的实际速度，数据类型为有符号32位整数
		int16_t TorqueActualValue;
	  // 操作模式显示，用于展示设备当前的操作模式，数据类型为有符号8位整数
    int8_t ModesOfOperationDisplay;
		int32_t FollowingErrorActualValue;
		uint16_t TouchProbeStatus;
		int32_t TouchProbePos1PosValue;
		int32_t TouchProbePos2PosValue;
    int32_t VelocityActualValue;
}PDO_Input;
#endif
void ecat_loop(void);
		
#ifdef EC_VER1
int ec_config_init(uint8 usetable);
int ec_config_map(void *pIOmap);
int ec_config_map_group(void *pIOmap, uint8 group);
int ecx_config_map_group(ecx_contextt *context, void *pIOmap, uint8 group);
int ec_config(uint8 usetable);
int ecx_myconfig_slave(ecx_contextt *context, uint16 slave, int timeout);
#endif

int ecx_config_init(ecx_contextt *context, uint8_t usetable);
int ecx_recover_slave(ecx_contextt *context, uint16_t slave, int timeout);

extern int  COUPLER_NUM;
extern uint8_t output_data[160];
extern uint8_t input_data[160];
extern uint8_t socket_mode;
extern uint16_t run_status;
extern uint16_t slave_status[10];	
extern uint8_t modules_status[10];

#ifdef __cplusplus
}
#endif

#endif
